<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="160px" v-loading="formLoading" :rules="rules">
    
      <el-form-item label="选择班级：" prop="className" clearable required>
        <el-select v-model="form.className" placeholder="请选择" @click="getClass"> 
          <el-option v-for="item in classList" :key="item.id" :label="item.className" :value="item.className"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学生姓名：" prop="stuName" clearable required>
        <el-input v-model="form.stuName"></el-input>
      </el-form-item>
       <el-form-item label="基站编号：" prop="voteBaseId" required>
        <el-input v-model="form.voteBaseId"></el-input>
      </el-form-item>
       <el-form-item label="答题器编号：" prop="voteIdFromVM" required>
        <el-input v-model="form.voteIdFromVM"></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="gender" required>
        <el-select v-model="form.gender" placeholder="性别" clearable>
          <el-option v-for="item in sexEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
       
      <el-form-item label="手机：" prop="stuPhone" required>
        <el-input v-model="form.stuPhone"></el-input>
      </el-form-item>
    <el-form-item label="备注：" prop="note">
        <el-input v-model="form.note"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { mapGetters, mapState, mapActions } from 'vuex'
// import Pagination from '@/components/Pagination'
// import studentApi from '@/api/student'
// import classApi from '@/api/class'

export default {
  data () {
    return {
      form: {
        id: null, 
        className: '', 
        classId: '',
        stuName: '',      
        gender: '',
        stuPhone: null,
        voteBaseId: null,
        voteIdFromVM: null,
        note: '' 
        
      },
      classList:[],
      formLoading: false,
      rules: {
        className: [
          { required: true, message: '请选择班级', trigger: 'change' }
        ],
        stuName: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' }
          ],
        voteBaseId: [
          { required: true, message: '基站编号', trigger: 'blur' }
          ],
        voteIdFromVM: [
          { required: true, message: '请输入学生答题器编号', trigger: 'blur' }
          ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        stuPhone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    let id = this.$route.query.id
    //let className = this.$route.query.className
    let _this = this
    
    this.getClass();
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      studentApi.selectStudent(id).then(re => {
        _this.form = re.response
        this.form.className = this.$route.query.className
        this.form.voteIdFromVM = re.response.voteId
        _this.formLoading = false
      })
    }
  },
  methods: {
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
         studentApi.createStudent(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/class/student/list')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    getClass() {
      this.formLoading = true;
      classApi.getClassPageList({ condition: null, pageIndex: 1, pageSize: 100 })
        .then(res => {
          this.formLoading = false;
          if (res.code == 1) {
            this.classList = res.response.list;
          }
        })
        .catch(res => {
          this.formLoading = false;
        });
    },
    resetForm () {
      this.$refs['form'].resetFields()
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      sexEnum: state => state.student.sexEnum,
    })
  }
}
</script>
